package com.camera2.dao;

import android.content.Context;
import android.hardware.Camera;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.net.Uri;
import android.os.Environment;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.camera2.http.SendImage;

import java.io.File;
import java.util.Calendar;

public class MovieRecorder implements SurfaceHolder.Callback {
    private MediaRecorder mRecorder;
    private SurfaceHolder mSurfaceHolder;
    private SurfaceView mSurfaceview;
    private Camera camera;
    private MediaPlayer mediaPlayer;
    private String path;
    private int flag;
//    private AutoFocusCallback myAutoFoucusCallback;



    public void init( SurfaceView mSurfaceview){
        this.mSurfaceview=mSurfaceview;
        SurfaceHolder holder = mSurfaceview.getHolder();
        holder.addCallback(this);

        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    }
    public void startVideo() {

        if (mediaPlayer != null) {
            mediaPlayer.stop();
            mediaPlayer.reset();
            mediaPlayer.release();
            mediaPlayer = null;
        }

        if (mRecorder == null) {
            mRecorder = new MediaRecorder();
        }

        camera = Camera.open(Camera.CameraInfo.CAMERA_FACING_BACK);
        if (camera != null) {
            camera.setDisplayOrientation(90);
            camera.unlock();
            mRecorder.setCamera(camera);
        }
        mRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
        mRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        mRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
        mRecorder.setVideoSize(640, 480);
        mRecorder.setVideoFrameRate(30);
        mRecorder.setVideoEncodingBitRate(6 * 1024 * 1024);
        mRecorder.setOrientationHint(90);
        mRecorder.setMaxDuration(30 * 1000);
        mRecorder.setPreviewDisplay(mSurfaceview.getHolder().getSurface());
        try {

            path = getSDPath();
            if (path != null) {
                File dir = new File(path + "/xiaomuyi");
                if (!dir.exists()) {
                    dir.mkdir();
                }
                path = dir + "/" + getDate() + ".mp4";
                SendImage.imagePath = path;
                mRecorder.setOutputFile(path);
                mRecorder.prepare();
                mRecorder.start();

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void palyVedio(Context context) {

        Uri uri = Uri.parse(path);
        if (null != mediaPlayer) {
            mediaPlayer.reset();
        }
        mediaPlayer = MediaPlayer.create(context, uri);
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mediaPlayer.setDisplay(mSurfaceHolder);
        try {
            mediaPlayer.prepare();
        } catch (Exception e) {
            e.printStackTrace();
        }
        mediaPlayer.start();
    }




    public static String getDate() {
        Calendar ca = Calendar.getInstance();
        int year = ca.get(Calendar.YEAR);
        int month = ca.get(Calendar.MONTH);
        int day = ca.get(Calendar.DATE);
        int minute = ca.get(Calendar.MINUTE);
        int hour = ca.get(Calendar.HOUR);
        int second = ca.get(Calendar.SECOND);

        String date = "" + year + (month + 1) + day + hour + minute + second;
//        Log.d(TAG, "date:" + date);

        return date;
    }

    public String getSDPath() {
        File sdDir = null;
        boolean sdCardExist = Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED);
        if (sdCardExist) {
            sdDir = Environment.getExternalStorageDirectory();
            return sdDir.toString();
        }

        return null;
    }
    private android.os.Handler handler = new android.os.Handler();
    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            handler.postDelayed(this, 1000);
        }
    };
    public void stopVideo() {
        try {
            handler.removeCallbacks(runnable);
            mRecorder.stop();
            mRecorder.reset();
            mRecorder.release();
            mRecorder = null;

            if (camera != null) {
                camera.release();
                camera = null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void destory(){
        if (mediaPlayer != null) {

            mediaPlayer.stop();
            mediaPlayer.reset();
            mediaPlayer.release();
            mediaPlayer = null;
        }
        if (null != mRecorder) {
            mRecorder.stop();
            mRecorder.reset();
            mRecorder.release();
            mRecorder = null;
        }
        if (camera != null) {
            camera.release();
            camera = null;
        }
    }
    @Override
    public void surfaceChanged(SurfaceHolder surfaceHolder, int arg1, int arg2,
                               int arg3) {
        mSurfaceHolder = surfaceHolder;
    }

    @Override
    public void surfaceCreated(SurfaceHolder surfaceHolder) {
        mSurfaceHolder = surfaceHolder;
        if (0 == flag) {
//            palyVedio();
        }
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder arg0) {
        mSurfaceview = null;
        mSurfaceHolder = null;
        handler.removeCallbacks(runnable);
        if (mRecorder != null) {
            mRecorder.release();
            mRecorder = null;

        }
        if (camera != null) {
            camera.release();
            camera = null;
        }
        if (mediaPlayer != null) {
            mediaPlayer.release();
            mediaPlayer = null;
        }
    }
}
